// BotQuestionModel.java
package com.agileboot.domain.bot.botQuestion.model;

import cn.hutool.core.bean.BeanUtil;
import com.agileboot.domain.bot.botQuestion.db.BotQuestionService;
import com.agileboot.domain.bot.botQuestion.command.AddBotQuestionCommand;
import com.agileboot.domain.bot.botQuestion.command.UpdateBotQuestionCommand;
import com.agileboot.domain.bot.botQuestion.db.BotQuestionEntity;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class BotQuestionModel extends BotQuestionEntity {

    private BotQuestionService service;

    public BotQuestionModel(BotQuestionService service) {
        this.service = service;
    }

    public BotQuestionModel(BotQuestionEntity entity, BotQuestionService service) {
        BeanUtil.copyProperties(entity, this);
        this.service = service;
    }

    public void loadFromAddCommand(AddBotQuestionCommand cmd) {
        BeanUtil.copyProperties(cmd, this, "id");
    }

    public void loadFromUpdateCommand(UpdateBotQuestionCommand cmd) {
        loadFromAddCommand(cmd);
        this.setId(cmd.getId());
    }

    public void checkQuestionUnique() {
        if (service.isQuestionDuplicated(this.getId(), this.getQuestion())) {
            throw new RuntimeException("问题内容重复：" + this.getQuestion());
        }
    }
}
